<template>
	<view class="page">
		<u-navbar title="我的活动" border @leftClick="$utils.back()" placeholder />
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view v-for="item in data" :key="item.id" class="contentItem" @click="jump(item)">
						<image mode="aspectFill" :src="item.conquer.thumb" />
						<view class="contentInfo">
							<u-text lines='1' :text='item.conquer.title' size='24rpx' />
							<u-text lines='1' :text='`基数：${item.weight}Kg`' size='24rpx' />
							<u-text lines='1' :text='`挑战宣言：${item.motto}`' size='24rpx' />
							<view>
								开始时间:{{item.conquer.start_at}}
							</view>
						</view>
						<view @click.stop="()=>{}">
							<u-icon name="more-dot-fill" size="50rpx" @click='openSelect(item)'></u-icon>
						</view>
					</view>
				</template>
				<template v-else>
					<view v-for="item in 8" :key="item" class="contentItem">
						<view class="Skeleton" style="width: 160rpx;height: 160rpx;" />
						<view class="contentInfo">
							<view style="height: 30rpx;width: 100%;" class="Skeleton" v-for="item in 3" :key="item" />
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<root-portal>
			<u-modal :show="modalShow" :showCancelButton='true' @close='cancel' @cancel='cancel' @confirm='update'>
				<view style="width: 100%;">
					<view class="popupTitle">
						<u-icon name='close' color='#fff' />
						<view>
							加入挑战
						</view>
						<u-icon name='close' @click='cancel' />
					</view>
					<view style="padding: 0 40rpx;">
						<u--form labelWidth='100' labelPosition="top" :model="form" :rules="rules" ref="uForm">
							<u-form-item label="体重:" prop="weight" borderBottom>
								<u--input v-model="form.weight" placeholder="请输入体重,注意单位为Kg" />
							</u-form-item>
							<u-form-item label="想说的话:" prop="motto" borderBottom>
								<u--input v-model="form.motto" placeholder="请输入想说的话" />
							</u-form-item>
						</u--form>
					</view>
				</view>
			</u-modal>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					weight: null,
					motto: null
				},
				rules: {
					'weight': [{
						required: true,
						message: '请输入体重',
						trigger: ['blur', 'change']
					}, {
						type: 'number',
						message: '请输入正确体重',
						trigger: ['blur', 'change']
					}, {
						validator: (rule, value, callback) => {
							return value > 0
						},
						message: '体重必须大于0',
						trigger: ['blur', 'change']
					}],
					'motto': {
						type: 'string',
						required: true,
						message: '请输入想说的话',
						trigger: ['blur', 'change']
					},
				},
				modalShow: false,
				data: [],
				loading: true,
				selectItem: {},
				selectShow: false,
				disabled: false
			}
		},
		computed: {
			columns() {
				return [{
					name: '编辑'
				}, ]
			}
		},
		methods: {
			update() {
				if (this.disabled) return
				this.$refs.uForm.validate().then(res => {
					this.disabled = true
					this.$api.Conquer.Record.Update(this.selectItem.id, this.form).then(res => {
						uni.$u.toast('加入成功')
						this.disabled = false
						this.modalShow = false
						this.$refs.paging.reload()
						this.cancel()
					}).catch(err => {
						this.disabled = false
						uni.$u.toast(err.data.message)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			cancel() {
				if (this.disabled) return
				this.$refs.uForm.resetFields()
				this.form = {
					weight:null,
					motto:null
				}
				this.modalShow = false
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '编辑') {
					this.modalShow = true
					this.form = {
						weight: this.selectItem.weight,
						motto: this.selectItem.motto
					}
					setTimeout(() => {
						this.$refs.uForm.setRules(this.rules)
					}, 500)
				}
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			jump(item) {
				if(!item.has_started){
					this.$utils.navigateTo({
						url:"/Conquer/detailNoStar?id="+item.id
					})
				}else if(item.is_public){
					this.$utils.navigateTo({
						url:"/Conquer/Detail?id="+item.id
					})
				}else{
					this.$utils.navigateTo({
						url:"/User/detail?conquer_id="+item.id+'&id='+item.user_id
					})
				}
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.My.Record(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.data)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 50rpx;
		width: 200rpx;
		position: fixed;
		margin: auto;
		left: 0;
		right: 0;
		bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		background: linear-gradient(to top, #3573be, #489aff);
	}

	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.contentItem {
		padding: 20rpx 0;
		display: flex;
		border-bottom: 1rpx solid #f5f5f5;

		image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 8rpx;
		}

		.contentInfo {
			flex: 1;
			margin: 0 20rpx;
			display: flex;
			flex-direction: column;
			height: 160rpx;
			justify-content: space-between;
		}

		.icon {
			background-color: #ddd;
			border-radius: 100rpx;
			padding: 8rpx 20rpx;
			display: inline-block;
			margin-right: 10rpx;
		}
	}
</style>